package udp_communication;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Server {
    public static void main(String[] args) throws IOException {
        System.out.println("服务端启动！！");
        //创建一个服务端对象 注册端口
        DatagramSocket socket = new DatagramSocket(6666);
        //创建一个数据包对象，用于接受数据
        byte[] buffer = new byte[1024*64];
        DatagramPacket pocket = new DatagramPacket(buffer, buffer.length);
        //接收数据
        socket.receive(pocket);
        //从字节数组中，把接收到的数据直接打印出来
        int len = pocket.getLength();
        String rs = new String(buffer, 0, len);
        System.out.println(rs);
        System.out.println(pocket.getAddress().getHostAddress());
        System.out.println(pocket.getPort());
        //释放资源！
        socket.close();

    }
}
